//
//  HomePageTableViewCell.m
//  InSuger
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "HomePageTableViewCell.h"

@implementation HomePageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self create];
    }
    return self;
}

- (void)create {
    
    self.backImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.backImageView];
    
    self.titleLabel = [[UILabel alloc] init];
    [self.backImageView addSubview:self.titleLabel];
    
    self.likeView = [[UIView alloc] init];
    [self.backImageView addSubview:self.likeView];
    
    self.likeButton = [[UIButton alloc] init];
    [self.likeView addSubview:self.likeButton];
    
    self.likeLabel = [[UILabel alloc] init];
    [self.likeView addSubview:self.likeLabel];
    
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.backImageView.layer.cornerRadius = 10;
    self.backImageView.layer.masksToBounds = YES;
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self).with.offset(CELLHEIGHT / 100 * 6);
        make.left.equalTo(self).with.offset(CELLWIDTH / 100 * 3);
        make.bottom.equalTo(self).with.offset(- CELLHEIGHT / 100 * 6);
        make.right.equalTo(self).with.offset(- CELLWIDTH / 100 * 3);
        
    }];
    

    self.titleLabel.textColor = [UIColor whiteColor];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backImageView.mas_top).with.offset(CELLHEIGHT / 100 * 60);
        make.left.equalTo(self.backImageView.mas_left).with.offset(CELLWIDTH / 100 * 3);
        make.bottom.equalTo(self.backImageView.mas_bottom).with.offset(- CELLHEIGHT / 100 * 6);
        make.right.equalTo(self.backImageView.mas_right).with.offset(- CELLWIDTH / 100 * 3);
        
    }];

    self.likeView.layer.cornerRadius = 10;
    self.likeView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.8];
    [self.likeView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backImageView.mas_top).with.offset(CELLHEIGHT / 100 * 7);
        make.left.equalTo(self.backImageView.mas_left).with.offset(CELLWIDTH / 100 * 78);
        make.bottom.equalTo(self.titleLabel.mas_top).with.offset(- CELLHEIGHT / 100 * 17);
        make.right.equalTo(self.backImageView.mas_right).with.offset(- CELLWIDTH / 100 * 3);
        
    }];
    

    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.likeView.mas_top).with.offset(0);
        make.left.equalTo(self.likeView.mas_left).with.offset(0);
        make.bottom.equalTo(self.likeLabel.mas_top).with.offset(0);
        make.right.equalTo(self.likeView.mas_right).with.offset(0);
        
        make.height.mas_equalTo(CELLHEIGHT / 100 * 25);
    }];
    
    
    
    self.likeLabel.textColor = [UIColor whiteColor];
    self.likeLabel.textAlignment = 1;
    self.likeLabel.font = [UIFont systemFontOfSize:12];
    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.likeButton.mas_bottom).with.offset(0);
        make.left.equalTo(self.likeView.mas_left).with.offset(0);
        make.bottom.equalTo(self.likeView.mas_bottom).with.offset(0);
        make.right.equalTo(self.likeView.mas_right).with.offset(0);
        
    }];
    
}

- (void)setModel:(HomePageTableViewModel *)model {
    
    if (_model != model) {
        
        [self.backImageView sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"PlaceHolder.png"]];
        self.titleLabel.text = model.title;
        [self.likeButton setImage:[UIImage imageNamed:@"like.png"] forState:UIControlStateNormal];
        self.likeLabel.text = [NSString stringWithFormat:@"%@", model.likes];
        
    }
    @weakify(self);
    [self addColorChangedBlock:^() {
        @strongify(self);
        self.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.normalBackgroundColor = [UIColor whiteColor];
        
    }];
    
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
